package com.tale.competition.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tale.competition.entity.User;
import com.tale.competition.service.MailService;
import com.tale.competition.service.UserService;
import com.tale.competition.common.util.JWTUtil;
import com.tale.competition.common.util.JedisUtil;
import com.tale.competition.common.util.MD5Util;
import com.tale.competition.common.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tale
 * @since 2020-02-16
 */
@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private JedisUtil jedistUtil;
    @Autowired
    private MailService mailService;

    @RequestMapping("/login")
    @ApiOperation("登录")
    @ResponseBody
    public R login(@RequestParam(value = "userName", defaultValue = "") String userName,
                   @RequestParam(value = "password", defaultValue = "") String password){

        System.out.println("========" + userName + "**&&" + password);

        if(userName.equals("") || userName == null){
            return R.error().message("请输入用户名");
        }else if (password.equals("") || password == null){
            return R.error().message("请输入密码");
        }


        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_name", userName);
        wrapper.eq("password", password);


        System.out.println("user:" + userName);

        User one = userService.getOne(wrapper);
        if (one != null){
            String token = JWTUtil.createToken(one.getId() + "", one.getUserName(), one.getUtype());

            jedistUtil.setToken(one.getId() + "", token, 60 * 60 * 24 * 6);

//            Claims claims = JWTUtil.decryptJWT(token);
//            claims.get()

            mailService.sendMail( one.getUserName() + " 您已经注册成功！");

            return R.ok().message("登录成功");
        }

        return R.error().message("密码错误");
    }

    @RequestMapping("/add")
    @ResponseBody
    @ApiOperation("添加用户")
    public R addUser(@RequestBody User user){
        System.out.println("===================" + user.getPassword());


//        if(user.getUserName().equals("") || user.getUserName() == null){
//            return R.error().message("请输入用户名");
//        }else if (user.getPassword().equals("") || user.getPassword() == null){

        if (user.getPassword().equals("") || user.getPassword() == null) {

            user.setPassword(MD5Util.saltEncryption(user.getPassword()));

            System.out.println(user.getPassword() + "==" + user.getUserName());

            boolean flag = userService.saveOrUpdate(user);

            if (flag) {
                return R.ok().message("添加用户成功");
            }
        }

        return R.error().message("添加用户失败");
    }
}

